package com.llkj.contractmanagement.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;


import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.main.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.logging.Logger;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by Administrator on 2016/4/12.
 */
public class ShareUtil {
    public static String TEST_IMAGE;
    private static final String FILE_NAME = "/share_hrcm_pic.jpg";

    // private static String title = "劳动者测试分享";
    // private static String url = "http://www.baidu.com";

//    /**
//     * 新浪微博分享
//     */
//    public static void shareSina(String title, String content, String url, String imageUrl) {
//        ShareParams sp = new ShareParams();
//        sp.setTitle(title);
//        sp.setText(title + "点击看详情" + url);
//        sp.setUrl(url);
//        sp.setTitleUrl(url);
//        if (TextUtils.isEmpty(imageUrl)) {
//            sp.setImagePath(TEST_IMAGE);
//        } else {
//            sp.setImageUrl(imageUrl);
//        }
//
//        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
//        weibo.setPlatformActionListener(new MyPlatformActionListener()); // 设置分享事件回调
//        weibo.SSOSetting(true);
////        weibo.isAuthValid();
//        // 执行图文分享
//        weibo.share(sp);
//        weibo.removeAccount();
//    }

//    /**
//     * 微信朋友圈分享
//     */
//    public static void shareFriends(String title, String content, String url,
//                                    String imageUrl) {
//        Platform plat = ShareSDK.getPlatform(WechatMoments.NAME);
//        ShareParams sp = new ShareParams();
//        sp.setShareType(Platform.SHARE_WEBPAGE);
//        sp.setTitle(title);
//        sp.setText(content);
//        sp.setTitleUrl(url);
//        sp.setUrl(url);
//        if (TextUtils.isEmpty(imageUrl)) {
//            sp.setImagePath(TEST_IMAGE);
//        } else {
//            sp.setImageUrl(imageUrl);
//        }
//        plat.setPlatformActionListener(new MyPlatformActionListener()); // 设置分享事件回调
//        plat.share(sp);
//
//    }

    /**
     * 微信分享
     */
    public static void shareWeiXin(String title, String content, String url, String imageUrl) {
        Platform plat = ShareSDK.getPlatform(Wechat.NAME);
        ShareParams sp = new ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(title);
        sp.setText(content);
        sp.setTitleUrl(url);
        sp.setUrl(url);
        if (TextUtils.isEmpty(imageUrl)) {
            sp.setImagePath(TEST_IMAGE);
        } else {
            sp.setImageUrl(imageUrl);
        }
        plat.setPlatformActionListener(new MyPlatformActionListener()); // 设置分享事件回调
        plat.share(sp);
    }

    /**
     * QQ分享
     */
    public static void shareQQ(String title, String content, String url, String imageUrl) {
        Platform plat = ShareSDK.getPlatform(QQ.NAME);
        ShareParams sp = new ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(title);
        sp.setText(content);
        sp.setTitleUrl(url);
        sp.setUrl(url);
        // 分享图片
        if (TextUtils.isEmpty(imageUrl)) {
            sp.setImagePath(TEST_IMAGE);
        } else {
            sp.setImageUrl(imageUrl);
        }
        plat.setPlatformActionListener(new MyPlatformActionListener()); // 设置分享事件回调
        plat.share(sp);
    }

    // /**
    // * QQ空间分享
    // */
    // public static void shareQZone() {
    // Platform plat = ShareSDK.getPlatform(QZone.NAME);
    // ShareParams sp = new ShareParams();
    // sp.setShareType(Platform.SHARE_WEBPAGE);
    // sp.setTitle(title);
    // sp.setTitleUrl("http://www.sina.com"); // 标题的超链接,这个是必填的，否则会分享失败
    // sp.setText(content);
    // sp.setTitleUrl(url);
    // sp.setSite("家校通");
    // sp.setImageUrl("");// 这里要传一个网络的图片，或者是sd卡里的本地图片（setImagePath）
    // sp.setSiteUrl("http://sharesdk.cn");
    // plat.setPlatformActionListener(new MyPlatformActionListener()); //
    // 设置分享事件回调
    // plat.share(sp);
    //
    // }

    static class MyPlatformActionListener implements PlatformActionListener {

        @Override
        public void onError(Platform arg0, int arg1, Throwable arg2) {
            // TODO Auto-generated method stub
            LogUtil.e("分享错误" + arg1 + "===" + arg2.toString());
            MyApplication.handler.post(new Runnable() {
                public void run() {
                    ToastUtil.makeShortText(MyApplication.getInstance(), "分享失败");
                }
            });

        }

        @Override
        public void onComplete(Platform arg0, int arg1,
                               HashMap<String, Object> arg2) {
            MyApplication.handler.post(new Runnable() {

                @Override
                public void run() {
                    ToastUtil.makeShortText(MyApplication.getInstance(), "分享成功");
                }
            });

        }

        @Override
        public void onCancel(Platform arg0, int arg1) {
            MyApplication.handler.post(new Runnable() {

                @Override
                public void run() {
                    ToastUtil.makeShortText(MyApplication.getInstance(), "取消分享");
                }
            });

        }
    }

    // 加载icon到本地好用于分享url没有的时候分享的图片
    public static void initImagePath(final Context context) {
        new Thread() {
            public void run() {
                try {
                    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                            && Environment.getExternalStorageDirectory().exists()) {
                        TEST_IMAGE = Environment.getExternalStorageDirectory()
                                .getAbsolutePath() + FILE_NAME;
                    } else {
                        TEST_IMAGE = context.getFilesDir().getAbsolutePath()
                                + FILE_NAME;
                    }
                    File file = new File(TEST_IMAGE);
                    if (!file.exists()) {
                        file.createNewFile();
                        Bitmap pic = BitmapFactory.decodeResource(
                                context.getResources(), R.mipmap.ic_launcher);
                        FileOutputStream fos = new FileOutputStream(file);
                        pic.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                    TEST_IMAGE = null;
                }
            }
        }.start();

    }
}
